练习一:自己定义一个reverse(s)函数,功能返回字符串s的倒序字符串。


In [16]:
def reverse(s):
    return s[::-1]

words=input("please enter a string:")
reverse(words)


please enter a string:kdnsg
Out[16]:
'gsndk'

练习二:写函数,根据给定符号和行数,打印相应直角三角形,等腰三角形及其他形式的三角形。


In [3]:
def PrintTriangle(s,n):
    knot="*"*2*n
    if s=="rt":
        for i in range(n):
            print(knot[:i:])
    elif s=="et":
        for i in range(n*2):
            if i%2!=0:
                print(' '*((2*n-i+1)//2)+knot[:i:]+' '*((2*n-i+1)//2))
            else :
                i
    else :
        print("你以为我会输给你看嘛?略略略")
        

figure=input('''请输入形状:
直角=rt 等腰=et 其他随便输反正我不打印''')
line=int(input("打印行数 :"))
PrintTriangle(figure,line+1)


请输入形状:
直角=rt 等腰=et 其他随便输反正我不打印rt
打印行数 :16

*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************

练习三:将任务4中的英语名词单数变复数的函数,尽可能的考虑多种情况,重新进行实现。


In [5]:
word=input("please enter a word")
if word=="have" :
    print("has")
elif word=="is" or word=="am" or word=="are" :
    print("is")
elif word.endswith('o') :
    print(word+"es")
elif word.endswith('s') or word.endswith('x') or word.endswith('sh') or word.endswith('ch') or word.endswith('e') :
    print(word+"es")
elif ((word.endswith('ay') or word.endswith('ey') or word.endswith('iy') or word.endswith('oy') or word.endswith('uy'))!=1) and word.endswith('y') :
    print(word[:-1]+"ies")
else :
    print(word+'s')


please enter a wordhero
heroes

练习四:写函数,根据给定符号,上底、下底、高,打印各种梯形。


In [ ]:

练习五:写函数,根据给定符号,打印各种菱形。


In [1]:
def Print(l,p):
    for i in range(p):
        print(' '*i+l*p)
  
knot=input('''请输入符号:
(回车结束)''')   
while knot!='':
    side=int(input("请输入边长:"))
    print("边长:{0},符号:{1},打印如下:".format(side,knot))
    Print(knot,side)
    knot=input('''请输入符号
(回车结束):''')


请输入符号:
(回车结束)&
请输入边长:5
边长:5,符号:&,打印如下:
&&&&&
 &&&&&
  &&&&&
   &&&&&
    &&&&&
请输入符号
(回车结束):0
请输入边长:12
边长:12,符号:0,打印如下:
000000000000
 000000000000
  000000000000
   000000000000
    000000000000
     000000000000
      000000000000
       000000000000
        000000000000
         000000000000
          000000000000
           000000000000
请输入符号
(回车结束):56
请输入边长:7
边长:7,符号:56,打印如下:
56565656565656
 56565656565656
  56565656565656
   56565656565656
    56565656565656
     56565656565656
      56565656565656
请输入符号
(回车结束):

练习六:与本小节任务基本相同,但要求打印回文字符倒三角形。


In [3]:
def plalindrome(line):
    for i in range( len(line)*2,0,-1):
        if i == 1:
            print(' '*(len(line)*2-1) +line[0])
        elif i%2 == 1:
            print(' '*(len(line)*2-i) + line[:i//2] + line[i//2] + line[i//2-1::-1])
        else:
            print(' '*(len(line)*2-i) + line[:i//2] + line[i//2-1::-1])

def main():
    text = '赏花归去马如飞'
    plalindrome(text)
    

if __name__ == '__main__':
    main()


赏花归去马如飞飞如马去归花赏
 赏花归去马如飞如马去归花赏
  赏花归去马如如马去归花赏
   赏花归去马如马去归花赏
    赏花归去马马去归花赏
     赏花归去马去归花赏
      赏花归去去归花赏
       赏花归去归花赏
        赏花归归花赏
         赏花归花赏
          赏花花赏
           赏花赏
            赏赏
             赏

In [ ]: